package com.zcf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zcf.common.json.Body;
import com.zcf.mapper.MallMapper;
import com.zcf.pojo.Agent;
import com.zcf.pojo.Mall;
import com.zcf.pojo.Notic;
import com.zcf.pojo.User;
import com.zcf.vo.out.LayuiJson;

import sun.management.resources.agent;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-03-18
 */
@Service
public class MallImpl {
	
	@Autowired
	MallMapper mm;
	/**
	 * 前端获取商城
	 * @param userid 
	 * @return
	 */
	public Body getMall(int userid) {
		Map<String, Integer> map ;
		List<Map<String, Integer>> list = new ArrayList<Map<String,Integer>>();
		Agent agent = new Agent().selectOne(new EntityWrapper<agent>().eq("userid", userid));
		EntityWrapper<Mall> w = new EntityWrapper<Mall>();
		w.setSqlSelect("money","diamond");
		Mall mall = new Mall();
		List<Mall> mall1 = mall.selectList(w);
		if(agent!=null){
			for (Mall mall2 : mall1) {
				map = new HashMap<String, Integer>();
				map.put("diamond", mall2.getDiamond());
				map.put("money", (int)Math.ceil(mall2.getMoney()*0.8));
				list.add(map);
			}
			return Body.newInstance(list);
		}else{
			if(mall1!=null){
				return Body.newInstance(mall1);
			}	
		}
	
		
		
		
		return Body.BODY_451;
	}
	/**
	 * 后端获取商城
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	
	public LayuiJson getMalls(int pageNum, int pageSize) {
		LayuiJson lj = new LayuiJson();
		EntityWrapper<Mall> w = new EntityWrapper<Mall>();
		List<String> list1 = new ArrayList<String>();
		List list = mm.selectPage(new Page<>(pageNum, pageSize), w);
		Integer num = mm.selectCount(w);
		if(list!=null){
			lj.setCode(0);
			lj.setCount(num);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setCount(num);
		lj.setData(null);
		lj.setMsg("暂无数据");
		return lj;
	}

	/**
	 * 更改商城钻石、金额
	 * @param mall
	 * @return
	 */
	public Body updatemall(Mall mall) {
		if(mall.getId() != null){
			if(mall.getDiamond() != null || mall.getMoney() != null){
				boolean b = mall.updateById();
				if(b){
					EntityWrapper<Mall> w = new EntityWrapper<Mall>();
					w.eq("id", mall.getId());
					Mall mall1 = mall.selectOne(w) ;
					return Body.newInstance(mall1);
				}
			}
			
		}
		
		return Body.BODY_451;
	}

	/**
	 * 删除商城一个记录
	 * @param mall
	 * @return
	 */
	public Body delete(Mall mall) {
		 if(mall.getId() != null){
			 boolean b = mall.deleteById();
			 if(b){
				return Body.BODY_200; 
			 }
			 
		 }
		return Body.BODY_451;
	}
	
	
	/**
	 * 添加商城钻石、金额
	 * @param mall
	 * @return
	 */
	public Body add(Mall mall) {
		if(mall.getDiamond() >0 && mall.getMoney() >0){
			EntityWrapper<Mall> w = new EntityWrapper<Mall>();
			Integer num = mm.selectCount(w);
			if(num!=null){
				num++;
			}
			mall.setId(num);
			boolean b = mall.insert();
			if(b){
				return Body.BODY_200;
			}
		}
		
		return Body.BODY_451;
	}

	


}
